package base;

import java.util.ArrayList;
import java.util.Collection;

public class CombinationGenerator {
	private int[] sizes;
	private int[] current;
	boolean hasMore;
	
	public CombinationGenerator(Collection<Collection<Object>> items)
	{
		if (items.isEmpty())
		{
			throw new IllegalArgumentException();
		}
		sizes = new int[items.size()];
		
		int i = 0;
		
		for (Collection<Object> list : items)
		{
			if (list.isEmpty())
			{
				throw new IllegalArgumentException();
			}
			sizes[i] = list.size();
			current[i] = 0;
			i++;
		}
		hasMore = true;
	}
	
	public CombinationGenerator(ArrayList<ArrayList<SingleShipMove>> items)
	{
		if (items.isEmpty())
		{
			throw new IllegalArgumentException();
		}
		sizes = new int[items.size()];
		current = new int[items.size()];
		int i = 0;
		
		for (Collection<SingleShipMove> list : items)
		{
			if (list.isEmpty())
			{
				throw new IllegalArgumentException();
			}
			sizes[i] = list.size();
			current[i] = 0;
			i++;
		}
		hasMore = true;
	}
	
	/**
	 * 
	 * 
	 * @return
	 */
	public void increaseCurrent()
	{
		current[sizes.length - 1]++;
		for (int i=sizes.length - 2; i >= 0; i--)
		{
			if (current[i+1] >= sizes[i+1])
			{
				current[i+1] = 0;
				current[i]++;
			}
			else
			{
				// since earlier index hasn't gone over the limit,
				// indexes beyond it shouldn't have either.
				break;
			}
		}
		
		if (current[0] >= sizes[0])
		{
			hasMore = false;
		}
	}
	
	public boolean hasMore()
	{
		return hasMore;
	}
	
	public int[] getIndices()
	{
		return current;
	}
}
